﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.Dataprogram;
using System.Windows;
using BLL.TcpUdp;
using Framework.Kit;
using System.Net;
using BLL.Action;

namespace Server
{
    /// <summary>
    /// 系统命令处理
    /// </summary>
    public class commandCtr
    {
        commandType cmdType;
        string data;
        string IP;
        DataproAnaly dataAny = new DataproAnaly();

        /// <summary>
        /// 构造函数
        /// </summary>
        public commandCtr(NetEventArgs e)
        {
            try
            {
                IP = IPAddress.Parse(((IPEndPoint)e.Client.ClientSocket.RemoteEndPoint).Address.ToString()).ToString();
                dataAny.uncodeData(e.Client.Datagram);
                this.cmdType = dataAny.CmdType;
                this.data = dataAny.DataBody;
                analyCmd(e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //根据命令类型处理相应的请求
        public void analyCmd(NetEventArgs e)
        {
            /*******是否需要返回当前的请求信息*******/
            bool isCondition = true;

            commandType cmdType = commandType.c000;
            string databBody = "";

            switch (this.cmdType)
            {

                /************返回客户端获取座位号的请求***********/
               
                case commandType.c003: //用户登录
                    User u = new User();

                    if (u.userLogin(data, IP))

                    {
                        cmdType = commandType.c004;
                        databBody = "登录成功了,这里是拍卖会信息";
                        auction a = new auction();
                    }
                    else
                    {
                        cmdType = commandType.c005;
                        databBody = u.rel;
                    }
                    break;
                default:
                    cmdType = commandType.c000;
                    databBody = "无效命令";
                    break;
            }

            //回复客户端信息
            if (isCondition)
            {
                try
                {
                    App.sendData(e.Client, cmdType, databBody);
                }
                catch (Exception ex)
                {
                    other.WriteErrLog("服务端发送信息", ex);
                }
            }
        }
    }
}
